/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__jolt_mar_read_UTF4.c                          */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/07 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"

#include "../../../src/module/tuxedojolt/config.h"
#include "../test_jolt_comm.h"
#include <tuxedojolt/jolt_core.h>
#include <tuxedojolt/jolt_comm.h>
#include <tuxedojolt/jolt_packet.h>
#include <apmtest_comm.h>
#include <tuxedojolt/jolt_mar.h>

int __jolt_mar_read_UTF4(L7session_jolt *p_st_jolt_sess
                        , L7_jolt_packet *p_st_jolt_packet
                        , char **pp_out
                        , unsigned int *p_ui_out_len);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_jolt_create_ct(&p_st_conn, TEST_JOLT_SRC_IP, TEST_JOLT_DST_IP, TEST_JOLT_SRC_PORT, TEST_JOLT_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);

    L7session_jolt_private *p_st_session_private = (L7session_jolt_private *) frct_get_session_private(p_st_session);
    L7session_jolt *p_st_sess = p_st_session_private->p_st_sess;
    L7_jolt_packet *p_st_jolt_packet = p_st_session_private->p_st_jolt_packet;

    char *p_bin = (char *) "4a4f4c540000000f00000090000000000000008c0000005000000001000000010000000400000000000000000000000000000000000000000000000000000000000100c80a00000a000000000c544553544d42535452494e470000000b0001020000080000000002000028000000000a000027000000000367626b0e0000090000000d100000010200000004d6d0b9fa";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_JOLT_SRC_IP, TEST_JOLT_DST_IP, TEST_JOLT_SRC_PORT, TEST_JOLT_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    CU_ASSERT_EQUAL(jolt_packet_unfragment(p_st_sess, p_st_packet, p_st_jolt_packet), APM_JOLT_CHECK_FRAGMENT_SUCCESS);

    CU_ASSERT_EQUAL(bytebuff_setPosition(p_st_jolt_packet->p_st_packet, 73), FR_SUCC);

    char *p_string = NULL;
    unsigned int ui_string_len = 0;
    CU_ASSERT_EQUAL(__jolt_mar_read_UTF4(p_st_sess, p_st_jolt_packet, &p_string, &ui_string_len), FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_string, "TESTMBSTRING");
    CU_ASSERT_EQUAL(ui_string_len, 12);

    jolt_free((void **) &p_string);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_jolt_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}


int add_suite__jolt_mar_read_UTF4(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __jolt_mar_read_UTF4()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
